﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Admin_Khuyen_Mai : System.Web.UI.Page
{
    DataProvider duLieu = new DataProvider();
    ListBox listBox = new ListBox();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_Danh_Muc();
        }
    }
    public void Load_Danh_Muc()
    {
        DataTable bangchude = null;
        bangchude = duLieu.Load_Du_Lieu_Admin_New("select * from khuyenmai order by id DESC", listBox);
        if (bangchude.Rows.Count != 0)
        {
            gvDuLieu.DataSource = bangchude;
            gvDuLieu.DataBind();
        }
        else
        {
            thongbaotrantrung.Text = "<font color='red'>Hiện Tại Trang Web Chưa Có KHUYẾN MÃI Nào.</font>";
        }
    }
    protected void gvDuLieu_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDuLieu.PageIndex = e.NewPageIndex;
        Load_Danh_Muc();
    }
    protected void gvDuLieu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDuLieu.EditIndex = -1;
        Load_Danh_Muc();
    }
    protected void gvDuLieu_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDuLieu.EditIndex = e.NewEditIndex;
        GridViewRow gvrow = gvDuLieu.Rows[e.NewEditIndex];
        gvrow.Cells[0].Height = 200;

        Load_Danh_Muc();
    }

    protected void gvDuLieu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = gvDuLieu.Rows[e.RowIndex].Cells[0].Text;
        string tieuDe = ((TextBox)gvDuLieu.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string moTaVanTat = ((TextBox)gvDuLieu.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string moTaChiTiet = ((TextBox)gvDuLieu.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string hinhAnh = ((TextBox)gvDuLieu.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

        if (!string.IsNullOrEmpty(tieuDe) & !string.IsNullOrEmpty(moTaVanTat) & !string.IsNullOrEmpty(moTaChiTiet) & !string.IsNullOrEmpty(hinhAnh))
        {
            listBox.Items.Clear();
            duLieu.MakeDuLieu(listBox, tieuDe);
            duLieu.MakeDuLieu(listBox, moTaVanTat);
            duLieu.MakeDuLieu(listBox, moTaChiTiet);
            duLieu.MakeDuLieu(listBox, hinhAnh);
            duLieu.MakeDuLieu(listBox, id);

            duLieu.Luu_Du_Lieu_Admin_New("update khuyenmai set tieude=?,motavantat=?,motachitiet=?,hinhAnh=? where id=?", listBox);
            gvDuLieu.EditIndex = -1;
            Load_Danh_Muc();
        }
        else
        {
            thongbaotrantrung.Text = "<font color='red'>Bạn Chưa Nhập Đầy Đủ Dữ Liệu.</font><br/><hr color='#008000' size='1'>";
        }
    }
    protected void gvDuLieu_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, gvDuLieu.Rows[e.RowIndex].Cells[0].Text);
        duLieu.Luu_Du_Lieu_Admin_New("delete from khuyenmai where id=?", listBox);
        File.Delete(Server.MapPath("..\\hinh_khuyen_mai\\") + gvDuLieu.Rows[e.RowIndex].Cells[4].Text);
        Response.Redirect("Khuyen_Mai.aspx");
    }

    private void Sap_Xep_Du_Lieu(string COT)
    {
        string LenhSql = "select * from khuyenmai";

        if (!string.IsNullOrEmpty(COT))
        {
            LenhSql = LenhSql + " Order by " + COT;
        }
        gvDuLieu.DataSource = duLieu.Load_Du_Lieu_Admin_New(LenhSql, listBox);
        gvDuLieu.DataBind();
    }
    protected void gvDuLieu_Sorting(object sender, GridViewSortEventArgs e)
    {
        Sap_Xep_Du_Lieu(e.SortExpression);
    }
    protected void luudanhmuc_Click(object sender, EventArgs e)
    {
        string tieuDe = tieude.Text.Trim();
        string moTaVanTat = motavantat.Text.Trim();
        string moTaChiTiet = motachitiet.Text.Trim();
        string hinhAnh = "";
        string ngayDangTin = DateTime.Now.ToShortDateString();
        if (hinhanh.HasFile & !string.IsNullOrEmpty(tieuDe) & !string.IsNullOrEmpty(moTaVanTat) & !string.IsNullOrEmpty(moTaChiTiet))
        {
            thongbaotrantrung.Text = "";
            //==== Kiểm Tra Và Upload Hình Khuyến Mãi====
            try
            {
                bool chophepupload = false;
                string phanMoRong = System.IO.Path.GetExtension(hinhanh.PostedFile.FileName);
                if (phanMoRong.Equals(".gif") || phanMoRong.Equals(".jpg") || phanMoRong.Equals(".png"))
                {
                    chophepupload = true;
                }
                if (chophepupload == false)
                {
                    thongbaotrantrung.Text = "<font color='red'>File Này Không Được Phép Upload.</font><br/><hr color='#008000' size='1'>";
                    return;
                }
                hinhanh.SaveAs(Server.MapPath("..\\hinh_khuyen_mai\\") + hinhanh.FileName);
                hinhAnh = hinhanh.PostedFile.FileName;
            }
            catch (Exception ex)
            {
                string message = "<SCRIPT LANGUAGE='JavaScript'>alert('" + ex.Message + "')</SCRIPT>";
                System.Web.HttpContext.Current.Response.Write(message);
                return;
            }

            // Nhập Khuyến Mãi Mới 

            listBox.Items.Clear();
            duLieu.MakeDuLieu(listBox, tieuDe);
            duLieu.MakeDuLieu(listBox, moTaVanTat);
            duLieu.MakeDuLieu(listBox, moTaChiTiet);
            duLieu.MakeDuLieu(listBox, hinhAnh);
            duLieu.MakeDuLieu(listBox, ngayDangTin);

            duLieu.Luu_Du_Lieu_Admin_New("insert into khuyenmai(tieude,motavantat,motachitiet,hinhanh,ngaydangtin) values(?,?,?,?,?)", listBox);
            Response.Redirect("Khuyen_Mai.aspx");
        }
        else
        {
            thongbaotrantrung.Text = "<font color='red'>Bạn Chưa Nhập Đầy Đủ Dữ Liệu.</font><br/><hr color='#008000' size='1'>";
        }
    }
}